﻿using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using Contracts.Entities;

namespace Model.MovieElements
{
    class DirectorConverter
    {
        /// <summary>
        /// Für das Lesen einer MovieDirectors-Relation
        /// </summary>
        public static IEnumerable<MovieElement> GetElements(EntityCollection<Directors> directors)
        {
            var results = new List<MovieElement>();
            foreach (var entity in directors)
                results.Add(GetElement(entity));

            return results;
        }

        /// <summary>
        /// Für das Lesen aller Directors aus Datenbank
        /// </summary>
        public static IEnumerable<MovieElement> GetElements(ObjectSet<Directors> directors)
        {
            var results = new List<MovieElement>();
            foreach (var entity in directors)
                results.Add(GetElement(entity));

            return results;
        }

        /// <summary>
        /// Für das Schreiben der Directors aus Tmdb-Quelle
        /// </summary>
        public static IEnumerable<MovieElement> GetElements(IEnumerable<TmdbPerson> directors)
        {
            var items = new List<MovieElement>();

            foreach (var item in directors)
            {
                items.Add(new MovieElement()
                {
                    Name = item.Name,
                    Type = MovieElementType.Director
                });
            }

            return items;
        }

        /// <summary>
        /// Für das Schreiben der Directors aus eigener Quelle
        /// </summary>
        public static EntityCollection<Directors> GetEntities(IEnumerable<MovieElement> directors)
        {
            var results = new EntityCollection<Directors>();

            foreach (var item in directors)
                results.Add(GetEntity(item));

            return results;
        }

        public static MovieElement GetElement(Directors director)
        {
            return new MovieElement()
            {
                Id = director.Id,
                Name = director.Name,
                Type = MovieElementType.Director
            };
        }

        public static Directors GetEntity(MovieElement director)
        {
            return new Directors()
            {
                Id = director.Id,
                Name = director.Name
            };
        }

    }
}
